home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSSTR2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-08
|
11KB
|
405 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsstr2.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains utility string functions.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Description: Check if all characters in a string are numeric.
// Parameters: pcsz String
// Returns: TRUE if all characters are numeric
//----------------------------------------------------------------------------
BOOL FN_E strisnumeric(PCSZ pcsz)
{
return strisvalid(pcsz, "[0-9]*");
}
//----------------------------------------------------------------------------
// Description: Check if all characters in a string are numeric or if
// the second character is an A,B, or C
// Parameters: pcsz String
// Returns: TRUE if all characters are numeric
//----------------------------------------------------------------------------
//BOOL FN_E strisnumeric2(PCSZ pcsz)
//{
// return strisvalid(pcsz, "[aAbBcC0-9]*");
//}
//----------------------------------------------------------------------------
// Description: Check if all characters in a string are valid phone
// TT characters (0..9 and A..Z except Q and Z)
// Parameters: pcsz String
// Returns: TRUE if all characters are valid
//----------------------------------------------------------------------------
BOOL FN_E strisphone(PCSZ pcsz)
{
return strisvalid(pcsz, "[a-zA-Z0-9~qQzZ]+");
}
//----------------------------------------------------------------------------
// Description: Check if all characters in a string are valid.
// Parameters: pcsz String
// pcszValid Valid characters for string.
// This string is a regular expression style
// statement, but it is very limited in complexity.
// Only ASCII chracters are allowed!
// No nested expressions are allowed.
// This algoritm is not recursive, so
// "a*a" is NOT allowed! ("a?a" is allowed).
// Returns: TRUE if all characters are numeric
//----------------------------------------------------------------------------
BOOL FN_E strisvalid(PCSZ pcsz, PCSZ pcszValid)
{
BYTE bFlag[128];
BOOL fFlag;
BOOL fOneOrMore;
BOOL fMany;
SIZET cCount;
CHAR chStart, chEnd;
Assert(pcsz && pcszValid);
while (pcszValid[0])
{
switch (toascii(pcszValid[0]))
{
case '\\': // Escape character
pcszValid++;
Assert(pcszValid[0]);
// Fall through
default: // Exact match
if (toascii(pcsz[0]) != toascii(pcszValid[0]))
return FALSE;
pcsz++;
pcszValid++;
break;
case '?': // Match any single
if (!pcsz[0]) // If no match, done
return FALSE;
pcsz++; // Skip character
return TRUE;
case '[':
pcszValid++; // Skip open bracket
fFlag = TRUE;
memset(bFlag, 0, sizeof(bFlag));
while (pcszValid[0] != ']')
{
Assert(pcszValid[0]);
if (pcszValid[0] == '~')
{
Assert(fFlag);
fFlag = FALSE;
pcszValid++;
}
else
{
if (pcszValid[0] == '\\')
{
pcszValid++;
Assert(pcszValid[0]);
}
chStart = toascii(pcszValid[0]);
pcszValid++;
if (pcszValid[0] == '-')
{
pcszValid++;
Assert(pcszValid[0]);
if (pcszValid[0] == '\\')
{
pcszValid++;
Assert(pcszValid[0]);
}
chEnd = toascii(pcszValid[0]);
pcszValid++;
for (; chStart <= chEnd; ++chStart)
bFlag[chStart] = (BYTE)fFlag;
}
else
bFlag[chStart] = (BYTE)fFlag;
}
}
pcszValid++; // Skip closing bracket
fOneOrMore = FALSE;
fMany = FALSE;
if (pcszValid[0] == '+') // Allow one or more characters to match
{
fOneOrMore = TRUE;
pcszValid++;
}
else if (pcszValid[0] == '*')
{
fMany = TRUE; // Allow any number of characters
pcszValid++;
}
if (fOneOrMore || fMany)
{
cCount = 0;
while (pcsz[0] && bFlag[toascii(pcsz[0])])
{
pcsz++;
cCount++;
}
if (!cCount && fOneOrMore)
return FALSE; // Need at least one character match
}
else // Only looking for a single charcter
{
if (!pcsz[0] || !bFlag[toascii(pcsz[0])])
return FALSE;
pcsz++;
}
break;
}
}
return pcsz[0] == '\0';
}
//----------------------------------------------------------------------------
// Description: Extract a long value from a string. Both signed and
// unsigned longs are recognized.
// Parameters: psz String to validate.
// pl Pointer to store long value at. If null, don't
// save value. 0L if return value is FALSE.
// Returns: TRUE if string represented a valid long.
//----------------------------------------------------------------------------
BOOL FN_E strvalidlong(PCSZ psz, PLONG pl)
{
static PSZ pszLong = "2147483647";
BOOL fMinus = FALSE;
BOOL fHex = FALSE;
SIZET cDigits = 0;
SIZET cZero = 0;
CHAR szBuf[11];
PSZ pszBuf = szBuf;
if (pl) // Set default value
pl[0] = 0L;
if (!psz || !psz[0]) // String must not be empty
return FALSE;
psz = strskipws(psz); // Skip leading whitespace
if (psz[0] == '0' && toupper(psz[1]) == 'X')
{
fHex = TRUE;
psz += 2;
}
else if (psz[0] == '+') // Extract sign
psz++;
else if (psz[0] == '-')
{
psz++;
fMinus = TRUE;
}
while (psz[0] && psz[0] == '0') // Remove leading zeroes
{
cZero++;
psz++;
}
while (psz[0] && isascii(psz[0]) &&
(isdigit(psz[0]) || (fHex && isxdigit(psz[0]))))
{ // Count digits and move them
++cDigits; // to temporary buffer
if (cDigits >= 11 || (fHex && cDigits > 8))
return FALSE;
*pszBuf++ = *psz++;
}
*pszBuf = '\0'; // Null terminate buffer
if (!cDigits && !cZero) // Must have at least one digit
return FALSE;
psz = strskipws(psz);
if (psz[0]) // Nothing allowed after last digit
return FALSE; // except whitespace
if (cDigits == 10 && !fHex) // Check if out of range
if (strcmp(szBuf, pszLong) > 0)
return FALSE;
if (pl)
if (cDigits) // Only leading zeroes
{
if (fHex)
pl[0] = (long)strtoul(szBuf, NULL, 16);
else
pl[0] = atol(szBuf);
if (fMinus)
pl[0] = -pl[0];
}
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Extract an signed short value from a string.
// Parameters: psz String to validate.
// ps Pointer to store short value at. If null, don't
// save value.
// Returns: TRUE if string represented a valid short.
//----------------------------------------------------------------------------
BOOL FN_E strvalidshort(PCSZ pcsz, PSHORT ps)
{
LONG l;
if (!strvalidlong(pcsz, &l))
return FALSE;
if (l < (LONG)MIN_SHORT || l > (LONG)MAX_SHORT)
return FALSE;
if (ps)
ps[0] = (SHORT)l;
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Extract an unsigned long value from a string.
// Parameters: psz String
// pul Pointer to store long value at. If null, don't
// save value. Contains the value 0L if the
// return value is FALSE.
// Returns: TRUE if string represented a valid unsigned long.
//----------------------------------------------------------------------------
BOOL FN_E strvalidulong(PCSZ psz, PULONG pul)
{
static PSZ pszULong = "4294967295";
SIZET cDigits = 0;
SIZET cZero = 0;
BOOL fHex = FALSE;
CHAR szBuf[11];
PSZ pszBuf = szBuf;
if (pul)
pul[0] = 0L;
if (!psz || !psz[0]) // String must not be empty
return FALSE;
psz = strskipws(psz); // Skip leading whitespace
if (psz[0] == '0' && toupper(psz[1]) == 'X')
{
fHex = TRUE;
psz += 2;
}
while (psz[0] && psz[0] == '0') // Remove leading zeroes
{
cZero++;
psz++;
}
while (psz[0] && isascii(psz[0])
&& (isdigit(psz[0]) || (fHex && isxdigit(psz[0]))))
{ // Count digits and move them
++cDigits; // to temporary buffer
// Too many digits for a valid ulong
if (cDigits >= 11 || (fHex && cDigits > 8))
return FALSE;
*pszBuf++ = *psz++;
}
*pszBuf = '\0';
if (!cDigits && !cZero)
return FALSE; // Must have at least one digit
psz = strskipws(psz);
if (psz[0]) // Nothing allowed after last digit
return FALSE; // except whitespace
if (cDigits == 10) // Check if out of range
if (strcmp(szBuf, pszULong) > 0)
return FALSE;
if (pul)
if (cDigits)
{
if (fHex)
pul[0] = strtoul(szBuf, NULL, 16);
else
pul[0] = strtoul(szBuf, NULL, 10);
}
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Extract an unsigned integer value from a string.
// Parameters: psz Pointer to string.
// pus Pointer to store short value at. If null, don't
// save value.
// Returns: TRUE if string represented a valid unsigned short
//----------------------------------------------------------------------------
BOOL FN_E strvalidushort(PCSZ psz, PUSHORT pus)
{
ULONG ul;
if (!strvalidulong(psz, &ul))
return FALSE;
if (ul > (ULONG)MAX_USHORT)
return FALSE;
if (pus)
pus[0] = (USHORT)ul;
return TRUE;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------